package afk.biglog;

import java.util.*;

public class BlockFilter
{
	private final List<String> blackList = new ArrayList<String>();
	private final List<String> whiteList = new ArrayList<String>();

	public List<String> getBlackList()
	{
		return blackList;
	}

	public List<String> getWhiteList()
	{
		return whiteList;
	}

	public boolean allow(final String line)
	{
		if (isAllowAll())
			return true;
		for (final String disAllow : blackList)
			if (line.contains(disAllow))
				return false;
		if (whiteList.size() > 0)
		{
			for (final String allow : whiteList)
				if (line.contains(allow))
					return true;
			return false;
		}
		return true;
	}

	private boolean isAllowAll()
	{
		return (blackList.size() == 0) && (whiteList.size() == 0);
	}

	@Override
	public String toString()
	{
		if (isAllowAll())
			return "";
		final StringBuffer buffer = new StringBuffer();
		final boolean first = true;
		for (final String allow : whiteList)
		{
			if (!first)
				buffer.append("\r\n");
			buffer.append("+").append(allow);
		}
		for (final String forbid : blackList)
		{
			if (!first)
				buffer.append("\r\n");
			buffer.append("-").append(forbid);
		}
		return buffer.toString();
	}
}
